package cn.edu.hqu.cst.android.cxy.test3;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity  implements View.OnClickListener{
    private TextView text2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        text2 = findViewById(R.id.text2);
        //通过ACTIVITY监听器监听
        Button button1 = findViewById(R.id.button1);
        button1.setOnClickListener(this);
        //通过内部类监听器监听
        Button button2 = findViewById(R.id.button2);
        button2.setOnClickListener(new MyClickListener());
        //通过匿名内部类监听器监听
        Button button3 = findViewById(R.id.button3);
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                text2.setText("通过匿名内部类监听器监听的按钮被点击了");
            }
        });
        //通过Lambda表达式作为监听器监听
        Button button4 = findViewById(R.id.button4);
        button4.setOnClickListener(view -> text2.setText("通过Lambda表达式作为监听器监听的按钮被点击了"));
        //通过外部类监听器监听
        Button button5 = findViewById(R.id.button5);
        button5.setOnClickListener(new OutClickListener(text2));
        Button button7 = findViewById(R.id.button7);
        button7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i1 = new Intent(MainActivity.this, SystemInformation.class);
                startActivity(i1);
            }
        });
        Button button8 = findViewById(R.id.button8);
        button8.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i2 = new Intent(MainActivity.this, Process.class);
                startActivity(i2);
            }
        });
    }
    public void clickHandle(View source){
        text2.setText("直接绑定监听器的按钮被点击了");
    }
    @Override
    public void onClick(View v)
    {
        text2.setText("通过ACTIVITY监听器监听的按钮被点击了");
    }
    class MyClickListener implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            text2.setText("通过内部类监听器监听的按钮被点击了");
        }
    }
}
class OutClickListener implements View.OnClickListener{
    private final TextView text2;
    public OutClickListener(TextView text2){
        this.text2 = text2;
    }
    @Override
    public void onClick(View v) {
        text2.setText("通过外部类监听器监听的按钮被点击了");
    }
}